//
//  ViewController.m
//  相册
//
//  Created by 王云 on 17/4/19.
//  Copyright © 2017年 王云. All rights reserved.
//

#import "ViewController.h"
#import "twoViewController.h"
@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property(nonatomic,strong) UIImageView *imageView;
@property(nonatomic,strong) UIButton *button;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *btn = [[UIButton alloc]init];
    [btn setTitle:@"点击" forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor redColor];
    btn.frame = CGRectMake(0, 100, 100, 100);
    [btn addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    self.button = btn;
    
    // 右边的按钮
    UIButton *rightBtn = [[UIButton alloc]init];
    [rightBtn setTitle:@"右边按钮" forState:UIControlStateNormal];
    rightBtn.backgroundColor = [UIColor yellowColor];
    rightBtn.frame = CGRectMake(150, 100, 100, 100);
    [rightBtn addTarget:self action:@selector(rightButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:rightBtn];
    
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 200, 200, 200)];
    imageView.backgroundColor = [UIColor blueColor];
    self.imageView = imageView;
    [self.view addSubview:imageView];
    
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)rightButtonClick
{
    twoViewController *vc = [[twoViewController alloc]init];
    [self presentViewController:vc animated:YES completion:nil ];
}

-(void)buttonClick
{
    
    UIImagePickerController *vc = [[UIImagePickerController alloc]init];
    // 如何获取图片,我们可以在代理里面获得
    vc.delegate = self;
    
    
    // 判断前后摄像头是否可用
    // 判断某个数据类型是否可用
    
    // 来源
    /*
     UIImagePickerControllerSourceTypePhotoLibrary,  图库
     UIImagePickerControllerSourceTypeCamera,  照相机
     UIImagePickerControllerSourceTypeSavedPhotosAlbum  相册
     */
    vc.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    // 是否可以编辑 选择图片之后有个框
    vc.allowsEditing = YES;
    [self presentViewController:vc animated:YES completion:nil];
    
    
    
}

// 一旦实现代理方法,uiimageVC就默认不会自动关闭了
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    NSLog(@"%@",info);
    // UIImagePickerControllerOriginalImage 没有编辑过的图片
    // UIImagePickerControllerEditedImage 编辑过的图片
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    self.imageView.image = image;
    [self.button setBackgroundImage:image forState:UIControlStateNormal];
    // 手动关闭uiimageVC
    [picker dismissViewControllerAnimated:YES completion:nil];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.button setBackgroundImage:nil forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
